﻿//#region Copyright
////       Copyright (c) 2009 - Antoine Jaussoin - http://www.jaussoin.com/blog/
//// 
////        This program is free software: you can redistribute it and/or modify
////        it under the terms of the GNU General Public License as published by
////        the Free Software Foundation, either version 3 of the License, or
////        (at your option) any later version.
//// 
////        This program is distributed in the hope that it will be useful,
////        but WITHOUT ANY WARRANTY; without even the implied warranty of
////        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
////        GNU General Public License for more details.
//// 
////        You should have received a copy of the GNU General Public License
////        along with this program.  If not, see <http://www.gnu.org/licenses/>.
//#endregion
//using System;
//using System.Linq;
//using FT.Architecture.Exceptions;
//using NHibernate;
//using NHibernate.Linq;

//namespace FT.Architecture.NHibernate.SessionManagement
//{
//    /// <summary>
//    /// <see cref="NHibernate"/> Session that can either be stateful or stateless
//    /// </summary>
//    public class PolySession
//    {
//        private readonly ISession _session;
//        private readonly IStatelessSession _statelessSession;

//        /// <summary>
//        /// Stateful constructor
//        /// </summary>
//        /// <param name="session"></param>
//        public PolySession(ISession session)
//        {
//            _session = session;
//            _statelessSession = null;
//        }

//        /// <summary>
//        /// Stateless constructor
//        /// </summary>
//        /// <param name="statelessSession"></param>
//        public PolySession(IStatelessSession statelessSession)
//        {
//            _statelessSession = statelessSession;
//            _session = null;
//        }

//        public PolySession()
//        {
//        }

//        /// <summary>
//        /// Create a Query, either from an <see cref="ISession"/> or a <see cref="IStatelessSession"/>
//        /// </summary>
//        /// <param name="query"></param>
//        /// <returns></returns>
//        public IQuery CreateQuery(string query)
//        {
//            if (_session != null)
//                return _session.CreateQuery(query);

//            return _statelessSession.CreateQuery(query);
//        }

//        /// <summary>
//        /// Create a Criteria, either from an <see cref="ISession"/> or a <see cref="IStatelessSession"/>
//        /// </summary>
//        /// <returns></returns>
//        public ICriteria CreateCriteria(Type persistentClass)
//        {
//            if (_session != null)
//                return _session.CreateCriteria(persistentClass);

//            return _statelessSession.CreateCriteria(persistentClass);
//        }

//        /// <summary>
//        /// Get
//        /// </summary>
//        /// <typeparam name="T"></typeparam>
//        /// <param name="id"></param>
//        /// <returns></returns>
//        public T Get<T>(object id)
//        {
//            if (_session != null)
//                return _session.Get<T>(id);

//            return _statelessSession.Get<T>(id);
//        }

//        /// <summary>
//        /// Get
//        /// </summary>
//        /// <typeparam name="T"></typeparam>
//        /// <param name="id"></param>
//        /// <param name="lockMode"></param>
//        /// <returns></returns>
//        public T Get<T>(object id, LockMode lockMode)
//        {
//            if (_session != null)
//                return _session.Get<T>(id, lockMode);

//            return _statelessSession.Get<T>(id, lockMode);
//        }

//        /// <summary>
//        /// Get a Linq provider
//        /// </summary>
//        /// <typeparam name="T"></typeparam>
//        /// <returns></returns>
//        public IQueryable<T> Linq<T>()
//        {
//            if (_session != null)
//                return _session.Linq<T>();

//            throw new UnitOfWorkException("The Linq provider is not available on a stateless Unit of Work. Please use a stateful Unit of Work instead.");
//        }

//        /// <summary>
//        /// Save or update
//        /// </summary>
//        /// <param name="obj"></param>
//        public void SaveOrUpdate(object obj)
//        {
//            if (_session != null)
//            {
//                _session.SaveOrUpdate(obj);
//                return;
//            }

//            throw new UnitOfWorkException("You can't save an entity on a stateless Unit of Work. Use a stateful Unit of Work.");
//        }

//        /// <summary>
//        /// Delete
//        /// </summary>
//        /// <param name="obj"></param>
//        public void Delete(object obj)
//        {
//            if (_session != null)
//            {
//                _session.Delete(obj);
//                return;
//            }

//            throw new UnitOfWorkException("You can't delete an entity on a stateless Unit of Work. Use a stateful Unit of Work.");
//        }

//        /// <summary>
//        /// Merge
//        /// </summary>
//        /// <param name="obj"></param>
//        /// <returns></returns>
//        public object Merge(object obj)
//        {
//            if (_session != null)
//            {
//                return _session.Merge(obj);
//            }

//            throw new UnitOfWorkException("You can't merge an entity on a stateless Unit of Work. Use a stateful Unit of Work.");
//        }

//        /// <summary>
//        /// Refreshes an entity from the database
//        /// </summary>
//        /// <param name="obj"></param>
//        /// <param name="lockMode"></param>
//        public void Refresh(object obj, LockMode lockMode)
//        {
//            if (_session != null)
//            {
//                _session.Refresh(obj, lockMode);
//                return;
//            }
            
//            _statelessSession.Refresh(obj, lockMode);
//        }

//        /// <summary>
//        /// Lock
//        /// </summary>
//        /// <param name="obj"></param>
//        /// <param name="lockMode"></param>
//        /// <returns></returns>
//        public void Lock(object obj, LockMode lockMode)
//        {
//            if (_session != null)
//            {
//                _session.Lock(obj, lockMode);
//                return;
//            }

//            throw new UnitOfWorkException("You can't lock an entity on a stateless Unit of Work. Use a stateful Unit of Work.");
//        }

//        /// <summary>
//        /// Flush
//        /// </summary>
//        public void Flush()
//        {
//            if (_session != null)
//                _session.Flush();
//        }

//        /// <summary>
//        /// Is Open
//        /// </summary>
//        public bool IsOpen
//        {
//            get
//            {
//                if (_session != null)
//                    return _session.IsOpen;

//                return true;
//            }
//        }

//        /// <summary>
//        /// Close session
//        /// </summary>
//        public void Close()
//        {
//            if (_session != null)
//                _session.Close();
//            else
//                _statelessSession.Close();
//        }

//        /// <summary>
//        /// Dispose
//        /// </summary>
//        public void Dispose()
//        {
//            if (_session != null)
//                _session.Dispose();
//            //else
//            //    _statelessSession.
//            //    _statelessSession.Dispose();
//        }

//        /// <summary>
//        /// Stateful session
//        /// </summary>
//        public ISession StatefulSession
//        {
//            get { return _session; }
//        }

//        /// <summary>
//        /// Stateless session
//        /// </summary>
//        public IStatelessSession StatelessSession
//        {
//            get { return _statelessSession; }
//        }

//        /// <summary>
//        /// If <see langword="true"/>, the session is stateful. Otherwise, it is stateless
//        /// </summary>
//        public bool IsStateful
//        {
//            get
//            {
//                return _session != null;
//            }
//        }
//    }
//}
